import axios from 'axios'
import {
  Message
} from 'element-ui'
import {
  getToken
} from '@/utils/auth'
// 创建axios
const service = axios.create({
  baseURL: `https://www.zheliguan.com/`, // 线上
  // baseURL: process.env.VUE_APP_BASE_API,
  timeout: 4000 // 响应超时
})
// 请求拦截
service.interceptors.request.use(
  config => {
    // 添加token
    config.headers['token'] = getToken()
    return config
  },
  error => {
    // 请求错误
    console.log() // for debug
    return Promise.reject(error)
  }
)
// 响应拦截
service.interceptors.response.use(
  response => {
    const res = response.data
    if (res.mark === '0') {
      Message({
        message: '数据加载成功',
        type: 'success',
        duration: 0.5 * 1000
      })
      return res
    } else if (res.mark === '2') {
      if (res.tip === '验证码不对') {
        Message({
          message: '验证码不对',
          type: 'error',
          duration: 1 * 1000
        })
      }
    } else if (res.mark === '1') {
      if (res.tip === '密码不对') {
        Message({
          message: '密码不对',
          type: 'error',
          duration: 1 * 1000
        })
      } else if (res.mark === '账号不存在') {
        Message({
          message: '账号不存在',
          type: 'error',
          duration: 1 * 1000
        })
      }
    } else if (res.mark === null) {
      Message({
        message: '密码修改成功,请重新登录',
        type: 'success',
        duration: 2 * 1000
      })
    }
  },
  error => {
    console.log('err' + error)
    Message({
      message: '数据返回失败',
      type: 'error',
      duration: 5 * 1000
    })
    return Promise.reject(error)
  }
)
export default service

// PublicKeyCredential
